package pro.shushi.pamirs.demo.core.service;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import pro.shushi.pamirs.business.api.model.PamirsDepartment;
import pro.shushi.pamirs.core.common.standard.service.impl.AbstractStandardModelService;
import pro.shushi.pamirs.demo.api.model.DemoItem;
import pro.shushi.pamirs.demo.api.model.DemoItemAttribute;
import pro.shushi.pamirs.demo.api.model.DemoItemLabel;
import pro.shushi.pamirs.demo.api.service.DemoItemService;
import pro.shushi.pamirs.framework.connectors.data.sql.query.LambdaQueryWrapper;
import pro.shushi.pamirs.framework.connectors.data.sql.update.LambdaUpdateWrapper;
import pro.shushi.pamirs.meta.annotation.Fun;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.api.dto.condition.Pagination;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Fun(DemoItemService.FUN_NAMESPACE)
public class DemoItemServiceImpl extends AbstractStandardModelService<DemoItem> implements DemoItemService {


    @Function
    @Override
    public DemoItem create(DemoItem data) {
        data.create();
        if(CollectionUtils.isNotEmpty(data.getItemLabels())){
            data.fieldSave(DemoItem::getItemLabels);
        }
        if(CollectionUtils.isNotEmpty(data.getItemAttributes())){
            data.fieldSave(DemoItem::getItemAttributes);
        }
        return data;
    }

    @Function
    @Override
    public DemoItem update(DemoItem data) {
        DemoItem exist = this.queryOne(data);
        if(data != null) data.updateById();
        //一对多保存
        if(CollectionUtils.isNotEmpty(data.getItemAttributes())){
            data.getItemAttributes().forEach(t->t.setItemId(data.getId()));
            data.fieldSave(DemoItem::getItemAttributes);
        }
        //多对多保存
        if(CollectionUtils.isNotEmpty(data.getItemLabels())){
            exist = exist.fieldQuery(DemoItem::getItemLabels);
            if(CollectionUtils.isNotEmpty(data.getItemLabels())) {
                exist.relationDelete(DemoItem::getItemLabels);
            }
            data.fieldSave(DemoItem::getItemLabels);
        }
        return data;
    }

    @Function
    @Override
    public Integer updateBatch(List<DemoItem> list) {
        return rawUpdateBatch(list);
    }

    @Function
    @Override
    public Integer updateByWrapper(DemoItem data, LambdaUpdateWrapper<DemoItem> wrapper) {
        return super.updateByWrapper(data, wrapper);
    }

    @Function
    @Override
    public DemoItem createOrUpdate(DemoItem data) {
        return super.createOrUpdate(data);
    }

    @Function
    @Override
    public List<DemoItem> delete(List<DemoItem> list) {
        return super.delete(list);
    }

    @Function
    @Override
    public DemoItem deleteOne(DemoItem data) {
        return super.deleteOne(data);
    }

    @Function
    @Override
    public Pagination<DemoItem> queryPage(Pagination<DemoItem> page, LambdaQueryWrapper<DemoItem> queryWrapper) {
        return super.queryPage(page, queryWrapper);
    }

    @Function
    @Override
    public DemoItem queryOne(DemoItem query) {
        DemoItem demoItem = super.queryOne(query);
        return demoItem;
    }

    @Function
    @Override
    public DemoItem queryOneByWrapper(LambdaQueryWrapper<DemoItem> queryWrapper) {
        DemoItem demoItem = super.queryOneByWrapper(queryWrapper);
        return demoItem;
    }

    @Function
    @Override
    public List<DemoItem> queryListByWrapper(LambdaQueryWrapper<DemoItem> queryWrapper) {
        List<DemoItem> list = super.queryListByWrapper(queryWrapper);
        return list;
    }

    @Function
    @Override
    public Long count(LambdaQueryWrapper<DemoItem> queryWrapper) {
        return super.count(queryWrapper);
    }
}
